package com.rent.view;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Scanner;

import com.rent.Impl.UserDaoImpl;
import com.rent.Impl.VipDaoImpl;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

import com.rent.bean.Rent;
import com.rent.bean.User;
import com.rent.bean.Vip;
import com.rent.dao.RecordDao;
import com.rent.dao.UserDao;
import com.rent.dao.VipDao;
import com.rent.Impl.RecordImpl;
import com.rent.Impl.UserDaoImpl;
import com.rent.Impl.VipDaoImpl;
import com.rent.utils.ViewUtils;

public class VipMenu {
	Scanner input=new Scanner(System.in);
	ViewUtils v = new ViewUtils();
	//用户信息接口
	UserDao userDao = new UserDaoImpl();
	VipDao vipDao = new VipDaoImpl();
	Option o = new Option();
	Scanner sn = new Scanner(System.in);
	VipDaoImpl ve=new VipDaoImpl();
	RecordDao re=new RecordImpl();
	/**
	 * VIP用户界面
	 */
	public void menu_vip(User u) {
		//判断一下用户的会员是否到期，到期后跳入普通用户
		boolean flag = true;
		try {
			flag = isVip(u);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		try {
			if(flag) {
				new OrdinaryMenu().menu_ordinary(u);
			}else {
				l:do {
					System.out.println("***********神州租车系统***********当前用户:"+u.getUname()+"----用户级别：vip用户");
					System.out.println("请选择业务：1、租车/还车 2、查询记录 3、续费会员 4、会员查询 5.余额充值 0.退出");			
					switch (sn.nextInt()) {
					case 0:
						System.out.println("系统退出");
						break l;
					case 1:
						o.leaseOrReturn(u);
						continue;
					case 2:
						o.show_rent(u);
						continue;
					case 3:
						xufei(u);
						continue;
					case 4:
						hcha(u);
						continue;
					case 5:
						topup(u);
						continue;
					default:
						System.out.println("输入不合法");
						continue;
					}
				}while(true);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} 
	}
	/**
	 * 判断是否使vip用户
	 * @throws ParseException 
	 */
	public boolean isVip(User u) throws ParseException {
		Date date = new Date(System.currentTimeMillis());
		Vip vip = vipDao.seleByUnameOrderLimittime(u.getUname());
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date parse = simpleDateFormat.parse(vip.getLimittime());
		//判断用户会员到期时间是否在现在时间之后
		if(date.after(parse)) {
			System.out.println("你的会员到期了");
			//会员到期，修改用户位普通用户
			userDao.updatePowerByUname(u.getUname());
		}else {
			return false;
		}
		return true;
	}
	public void cha(User u) {
		List<Rent> li=re.selectw(u.getUname());
		System.out.println("编号\t用户名\t车辆编号\t租车时间\t\t\t还车时间\t\t\t费用");
		for(Rent r:li) {
			System.out.println(r.getRid()+"\t"+r.getUname()+"\t "+r.getCid()+"\t"+r.getRtime()+"\t"+r.getBtime()+"\t"+r.getMoney());
		}
	}
	public void hcha(User u) {
		VipDao vip=new VipDaoImpl();
		List<Vip> li=vip.huiUname(u.getUname());
		for(Vip l:li) {
			System.out.println(l.getVid()+"\t"+l.getUname()+"\t"+l.getOpentime()+"\t"+l.getLimittime());
		}
	}
	public void xufei(User u) {
		VipDao vip=new VipDaoImpl();
		List<Vip> li=vip.seleByUname(u.getUname());
		for(Vip l:li) {
			System.out.println("会员到期时间为:"+l.getLimittime());
		}
		System.out.println("1.续费月卡(30天) 2.续费年卡(365天) 3.前往租车");
		int xuan=input.nextInt();
		switch(xuan) {
		case 1:
			sanshi(u);
			break;
		case 2:
			sanliuwu(u);
			break;
		case 3:
			break;
		}
	}
	public void sanshi(User u) {
		int gg=re.gengxin(u.getUname());
		if(gg!=0) {
		SimpleDateFormat simp=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date=new Date();
		Date ldate=null;
		Vip vip=vipDao.shicha(u.getUname());
		try {
			ldate=simp.parse(vip.getLimittime());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Calendar ca = Calendar.getInstance();
		String opentime=simp.format(date);//续费时间
		ca.setTime(ldate);//到期时间
		
		
		ca.add(Calendar.DATE, 30);//月卡加三十天
		String limittime=simp.format(ca.getTime());
		Vip vi=new Vip(0,u.getUname(),opentime,limittime);
		ve.newVip(u, vi);
		System.out.println("月卡续费成功！会员有效期至："+limittime);
		System.out.println("请重新登录:");
		Menu men=new Menu();
		men.init();
		}else {
			System.out.println("您的余额不足");
		}
	}
	public void sanliuwu(User u) {
		int gg=re.gengx(u.getUname());
		if(gg!=0) {
		SimpleDateFormat simp=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date=new Date();
		Date ldate=null;
		Vip vip=vipDao.shicha(u.getUname());
		try {
			ldate=simp.parse(vip.getLimittime());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Calendar ca = Calendar.getInstance();
		String opentime=simp.format(date);//续费时间
		ca.setTime(ldate);//到期时间
		
		
		ca.add(Calendar.DATE, 365);//月卡加三十天
		String limittime=simp.format(ca.getTime());
		Vip vi=new Vip(0,u.getUname(),opentime,limittime);
		ve.newVip(u, vi);
		System.out.println("月卡续费成功！会员有效期至："+limittime);
		Menu men=new Menu();
		men.init();
		}else {
			System.out.println("您的余额不足");
		}
	}
	public void topup(User u) {
		User us=new User();
		us.setUname(u.getUname());
		System.out.println("请输入充值金额:");
		us.setMoney(input.nextDouble());
		
		int chong=vipDao.topup(us);
		if(chong==0) {
			System.out.println("充值成功");
			User us1=re.yuer(u.getUname());
			System.out.print("您的账户余额为:");
			System.out.println(us1.getMoney());
		}
	}

}
